/*
 * Copyright (c) 2009, Hamish Morgan. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the University of Sussex nor the names of its
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package locusts.client.renderer;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.AffineTransform;
import locusts.common.entities.Entity;
import locusts.lib.geom.Shapes;
import locusts.lib.geom.Triangle;

/**
 *
 * @author hamish
 */
public class BasicEntityRenderer implements EntityRenderer {

    private static final Color DEFAULT_STROKE_COLOR = Color.BLACK;
    private static final Color DEFAULT_FILL_COLOR = Color.LIGHT_GRAY;
    private static final Color DEFAULT_TEXT_COLOR = Color.BLACK;
    private static final Stroke DEFAULT_STROKE = new BasicStroke(1);
    private static final String DEFAULT_FONT_NAME = "Arial";
    private static final int DEFAULT_FONT_STYLE = Font.PLAIN;
    private static final int DEFAULT_FONT_SIZE = 10;
    private static final boolean DEFAULT_SHOW_ID = false;
    private static final boolean DEFAULT_SHOW_ENERGY = false;
    //
    private Color strokeColor;
    private Color fillColor;
    private Color textColor;
    private double radius;
    private Shape shape;
    private boolean showId;
    private Stroke stroke;
    private Font font;
    private boolean showEnergy;

    public BasicEntityRenderer() {
        strokeColor = DEFAULT_STROKE_COLOR;
        fillColor = DEFAULT_FILL_COLOR;
        textColor = DEFAULT_TEXT_COLOR;
        shape = new Triangle(0, 0, 20, 13);
        radius = Shapes.calculateMaxRadius(shape);
        showId = DEFAULT_SHOW_ID;
        stroke = DEFAULT_STROKE;
        font = new Font(DEFAULT_FONT_NAME, DEFAULT_FONT_STYLE,
                DEFAULT_FONT_SIZE);
        showEnergy = DEFAULT_SHOW_ENERGY;
    }

    public void render(final Graphics2D g, final Entity e) {
        final AffineTransform saveAT = g.getTransform();
        g.translate(e.getX(), e.getY());
        g.rotate(e.getOrientation());

        g.setColor(fillColor);
        g.fill(shape);
        g.setStroke(stroke);
        g.setColor(strokeColor);
        g.draw(shape);

        if (showEnergy || showId) {
            g.rotate(-e.getOrientation());
            if (showEnergy)
                renderEnergyBar(g, e);
            if (showId)
                renderId(g, e);
        }

        g.setTransform(saveAT);
    }

    protected void renderId(final Graphics2D g, final Entity e) {
        g.setFont(font);
        final String str = "#" + Integer.toString(e.getId());
        final FontMetrics fm = g.getFontMetrics();
        final float xoffset = -fm.stringWidth(str) / 2;
        final float yoffset = (int) (radius + fm.getHeight());
        g.setColor(textColor);
        g.drawString(str, xoffset, yoffset);
    }

    protected void renderEnergyBar(final Graphics2D g, final Entity e) {
        final int x0 = (int) -radius;
        final int y0 = (int) -(radius + 4);
        final int x2 = (int) radius;
        final int x1 = x0 + (int) ((x2 - x0) * e.getEnergy() / 100d);

        g.setColor(Color.DARK_GRAY);
        g.fillRect(x0 - 1, y0 - 1, (x2 - x0) + 3, 3);
        g.setColor(Color.GREEN);
        g.drawLine(x0, y0, x1, y0);
        if (x1 != x2) {
            g.setColor(Color.RED);
            g.drawLine(x1, y0, x2, y0);
        }
    }

    public Color getFillColor() {
        return fillColor;
    }

    public void setFillColor(final Color fillColor) {
        this.fillColor = fillColor;
    }

    public Font getFont() {
        return font;
    }

    public void setFontFamily(final String name) {
        font = new Font(name, font.getStyle(), font.getSize());
    }

    public void setFontSize(final int size) {
        font = font.deriveFont(size);
    }

    public void setFontStyle(final int style) {
        font = font.deriveFont(style);
    }

    public void setFont(final Font font) {
        this.font = font;
    }

    public double getRadius() {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }
    

    public Shape getShape() {
        return shape;
    }

    public void setShape(final Shape shape) {
        this.shape = shape;
        radius = Shapes.calculateMaxRadius(shape);
    }

    public boolean isShowId() {
        return showId;
    }

    public void setShowId(final boolean showId) {
        this.showId = showId;
    }

    public Stroke getStroke() {
        return stroke;
    }

    public void setStroke(final Stroke stroke) {
        this.stroke = stroke;
    }

    public Color getStrokeColor() {
        return strokeColor;
    }

    public void setStrokeColor(final Color strokeColor) {
        this.strokeColor = strokeColor;
    }

    public Color getTextColor() {
        return textColor;
    }

    public void setTextColor(final Color textColor) {
        this.textColor = textColor;
    }

    public boolean isShowEnergy() {
        return showEnergy;
    }

    public void setShowEnergy(boolean showEnergy) {
        this.showEnergy = showEnergy;
    }
}
